<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog :title="!dataForm.alId ? '添加' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
      <el-form-item label="代理名称" prop="agentName">
        <el-input v-model="dataForm.agentName" placeholder="代理名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="agentSort">
        <el-input v-model="dataForm.agentSort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/agentlevel'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        alId: 0,
        agentName: '',
        agentSort: '',
        createTime: '',
        updateTime: '',
        remark: ''
      },
      dataRule: {
        agentName: [
          { required: true, message: '代理名称为必填项', trigger: 'blur' }
        ],
        agentSort: [
          { required: true, message: '排序为必填项', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间为必填项', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间为必填项', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.alId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.alId) {
          api.agentlevelDetailApi(this.dataForm.alId).then(res => {
            this.dataForm = res;
          })
        }
      })
    },
    // 表单数据提交
    dataSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.alId == 0) {
            api.AgentLevelCreateApi(this.dataForm).then(res => {
              // TODO 保存数据
              this.$emit('refreshDataList', 'submit')
            });
          } else {
            api.agentlevelUpdateApi(this.dataForm).then(res => {
              // TODO 保存数据
              this.$emit('refreshDataList', 'submit')
            });
          }
        }
      })
    }
  }
}
</script>